package topin.facecat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import facecat.topin.core.FCFile;
import facecat.topin.core.FCNative;
import facecat.topin.core.FCPaint;
import facecat.topin.core.FCSize;
import facecat.topin.core.FCSizeF;
import facecat.topin.core.FCTran;
import facecat.topin.sock.FCClientSocket;
import topin.facecat.Android.FCUIView;
import topin.facecat.Product.Service.DataCenter;
import topin.facecat.facecat.R;

import android.Manifest;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import java.io.DataInputStream;
import java.io.InputStream;
import static android.content.pm.PackageManager.PERMISSION_DENIED;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    /**
     * 读取XML
     * @param FileId
     * @return
     */
    public static String getXMlString(MainActivity activity, int FileId) {
        InputStream in = activity.getResources().openRawResource(FileId);
        DataInputStream din = null;
        try {
            din = new DataInputStream(in);
            byte[] bytes = new byte[din.available()];
            din.read(bytes);
            String ret = new String(bytes, "UTF-8");
            din.close();
            bytes = null;
            return ret;
        } catch (Exception e) {
            try {
                din.close();
            } catch (Exception e2) {
                return null;
            }
            return null;
        }
    }

    /*
     * 主视图静态对象
     */
    public static MainActivity m_mainActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (m_mainActivity != null && m_mainActivity != this){
            finish();
            return;
        }
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        m_mainActivity = this;
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
        FCClientSocket.noThread = false;
        String userPath = DataCenter.getUserPath();
        if(!FCFile.isDirectoryExist(userPath)){
            FCFile.createDirectory(userPath);
        }
        setUserPath(DataCenter.getUserPath());
        String xmlStr = getXMlString(this, R.raw.mainframe);
        String resStr = setKeyValue("mainframe," + xmlStr);
        FCUIView view = new FCUIView(getBaseContext());
        view.setAllowScroll(false);
        view.setCppName("111111");
        view.onLoad();
        //view.setLoadUI(m_chatMainFrame);
        setContentView(view);
    }

    public static native String createNative(String cmd);

    public static native String deleteNative(String cmd);

    public static native String onPaint(String cmd);

    public static native String onTouchBegin(String cmd);

    public static native String onTouchMove(String cmd);

    public static native String onTouchEnd(String cmd);

    public static native String updateNative(String cmd);

    public static native String setKeyValue(String cmd);

    public static native String onTimer(String cmd);

    public static native String setUserPath(String cmd);

    public static native String getTextInfo(String cmd);

    public static native String setTextInfo(String cmd);

    public static void invalidate(String cppName){
        if(FCUIView.m_nativeCpps.containsKey(cppName)){
            FCUIView uiView = FCUIView.m_nativeCpps.get(cppName);
            uiView.getHost().invalidate();
        }
    }

    public static String textSize(String cppName, String text, String font, int width){
        if(FCUIView.m_nativeCpps.containsKey(cppName)){
            FCUIView uiView = FCUIView.m_nativeCpps.get(cppName);
            FCNative inative = uiView.getNative();
            FCPaint paint = inative.getPaint();
            FCSize tSize = paint.textSize(text, FCTran.strToFont(font), width);
            return FCTran.sizeToStr(tSize);
        }else{
            return "0,0";
        }
    }

    public static String textSizeF(String cppName, String text, String font, int width) {
        if(FCUIView.m_nativeCpps.containsKey(cppName)){
            FCUIView uiView = FCUIView.m_nativeCpps.get(cppName);
            FCNative inative = uiView.getNative();
            FCPaint paint = inative.getPaint();
            FCSizeF tSize = paint.textSizeF(text, FCTran.strToFont(font), width);
            return FCTran.floatToStr(tSize.cx) + "," + FCTran.floatToStr(tSize.cy);
        }else{
            return "0,0";
        }
    }
}